package com.gw.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gw.common.model.Admin;
import com.gw.common.model.Powers;
import com.gw.base.BaseController;
import com.gw.interceptor.LoginInterceptorAdmin;
import com.gw.service.SystemService;
import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

@Before(LoginInterceptorAdmin.class)
public class Manager extends BaseController{
	SystemService systemService = enhance(SystemService.class);
	private final String table_schema = "transportdepartment";
	private final String table_comment = "TABLE_COMMENT";
	
	public void index(){
		String table_name = getPara(0);
		List<Record> col = systemService.getTableCol(table_schema, table_name);
		Record tableInfo = systemService.getTableInfo(table_schema, table_name);
		setAttr("col", col);
		setAttr("title", tableInfo.getStr(table_comment));
		setAttr("table_name", table_name);
		render("table.html");
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void table(){
		int pageNumber = getParaToInt("curPage");
		int pageSize = getParaToInt("pageSize");
		String sortName = getPara("sortName");
		String sortOrder = getPara("sortOrder");
		StringBuilder sqlExceptSelect = new StringBuilder();
		String tableName = getPara(0);
		sqlExceptSelect.append("from "+ tableName +" where 1=1 ");
		if (StrKit.notBlank(sortName)) {
			sqlExceptSelect.append("order by " + sortName + " " + sortOrder);
		}
		
		Page<Record> page = Db.use("mysql").paginate(pageNumber, pageSize, "select *", sqlExceptSelect.toString());
		
		Map m = new HashMap();
		m.put("success", true);
		m.put("totalRows", page.getTotalRow());
		m.put("curPage", pageNumber);
		m.put("data", page.getList());
		
		renderJson(m);
	}
	
	
	public void showAdd(){
		String table_name = getPara(0);
		setAttr("table_name", table_name);
		render(table_name + "-add.html");
	}
	
	
	public void add(){
		String table_name = getPara(0);
		if (table_name.equals(Admin.TABLENAME)) {
			getModel(Admin.class).save();
		}
		if (table_name.equals(Admin.TABLENAME)) {
			getModel(Powers.class).save();
		}
		redirect("/manager/" + table_name);
	}
	
	
	public void edit(){
		String table_name = getPara(0);
		int id = getParaToInt(1);
		Record record = Db.use("mysql").findById(table_name, id);
		setAttr("table_name", table_name);
		setAttr("record", record);
		render(table_name + "-edit.html");
	}
	
	
	public void update(){
		String table_name = getPara(0);
		if (table_name.equals(Powers.TABLENAME)) {
			getModel(Powers.class).update();
		}
		if (table_name.equals(Admin.TABLENAME)) {
			getModel(Admin.class).update();
		}
		redirect("/manager/" + table_name);
	}
	
	
	public void delete(){
		String table_name = getPara(0);
		int id = getParaToInt(1);
		boolean b = false;
		if (table_name.equals(Powers.TABLENAME)) {
			b = Powers.dao.deleteById(id);
		}
		if (table_name.equals(Admin.TABLENAME)) {
			b = Admin.dao.deleteById(id);
		}
		renderJson(b);
	}
	
	
	public void deleteSome(){
		String table_name = getPara(0);
		String pri = null;
		if (table_name.equals(Powers.class)) {
			pri = Powers.dao.getPrimaryKey()[0];
		}
		if (table_name.equals(Admin.class)) {
			pri = Admin.dao.getPrimaryKey()[0];
		}
		String ids = getPara("ids");
		Db.use("mysql").update("delete from " + table_name + " where "+ pri +" in ("+ ids + ")");
		boolean b = true;
		renderJson(b);
	}
	

}
